﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace VietKinsNutrition.Data
{
    public class ClsKhauphandinhduongtrongtuanDAL : ClsConnection
    {
        private DataTable _Table;
        private DataRow _Row;


        public DataTable Table
        {
            set { _Table = value; }
            get { return _Table; }
        }

        #region Template
        public DataTable GetTable(DateTime StartDay, DateTime FinishDay, string[] Day)
        {
            int j = 1;
            OleDbConnection cn = ClsConnection.OleDBConnect();
            DataTable _DataTable = new DataTable();

            OleDbCommand _command = new OleDbCommand("PrcTblDm_Tonghopdinhduong", cn);
            //_command.CommandType = CommandType.StoredProcedure;
            try
            {
                _command.Parameters.Add("@StartDay", OleDbType.Date).Value = StartDay;
                _command.Parameters.Add("@FinishDay", OleDbType.Date).Value = FinishDay;
                _command.ExecuteNonQuery();
                OleDbDataAdapter _Adapter = new OleDbDataAdapter(_command);
                _Adapter.Fill(_DataTable);
                cn.Close();
            }
            catch (System.Exception ex)
            { MessageBox.Show("Lỗi " + ex.Message); }
            if (_DataTable.Rows.Count > 0)
            {
                foreach (DataRow dr in _DataTable.Rows)
                {
                    string[] _Temp = dr["NgayNX"].ToString().Split('/');
                    string _Ngay = (int.Parse(_Temp[1])).ToString() + "/" + (int.Parse(_Temp[0])).ToString(); //MessageBox.Show(_Ngay);
                    for (int i = 0; i < Day.Length; i++)
                    {
                        if (_Ngay == Day[i])
                        {
                            if (this.CheckData(_Table, dr["Tenthucan"].ToString()))
                            {
                                _Row = _Table.NewRow();
                                _Row["STT"] = j;
                                _Row["Tenthucan"] = dr["Tenthucan"].ToString();
                                _Row["Tendonvitinh"] = dr["Tendonvitinh"].ToString();
                                _Row[_Ngay.Trim()] = 10;
                                _Table.Rows.Add(_Row);
                                j = j + 1;
                                break;
                            }
                            else
                            {
                                this.UpdateData(_Table, dr["Tenthucan"].ToString(), _Ngay, dr["Soluong"].ToString());
                                break;
                            }
                        }
                    }
                }
            }
            return _Table;
        #endregion
        }
        public DataTable ReturnTable(int Option, DateTime StartDay, DateTime FinishDay, string[] Day)
        {
            ClsTblDm_NamhocDAL ClsTblDm_Namhocdal = new ClsTblDm_NamhocDAL();
            int j = 1;
            OleDbConnection cn = ClsConnection.OleDBConnect();
            DataTable _DataTable = new DataTable();
            OleDbCommand _command;
            if (Option == 1)
            {
                _command = new OleDbCommand(QueryString.PRCTBLDM_TONGHOPDINHDUONG1.Replace("@StartDay", "#" + StartDay.ToString("MM/dd/yyyy") + "#").Replace("@FinishDay", "#" + FinishDay.ToString("MM/dd/yyyy") + "#"), cn);
            }
            else
            {
                _command = new OleDbCommand(QueryString.PRCTBLDM_TONGHOPDINHDUONG2.Replace("@StartDay", "#" + StartDay.ToString("MM/dd/yyyy") + "#").Replace("@FinishDay", "#" + FinishDay.ToString("MM/dd/yyyy") + "#").Replace("@NamhocId", "" + ClsTblDm_Namhocdal.ReturnNamhocId() + ""), cn);
            }
            //_command.CommandType = CommandType.StoredProcedure;
            try
            {
                //command.Parameters.Add("@Option", OleDbType.Integer).Value =Option;
                //_command.Parameters.Add("@StartDay", OleDbType.Date).Value = StartDay;
                //    _command.Parameters.Add("@FinishDay", OleDbType.Date).Value = FinishDay;
                _command.ExecuteNonQuery();
                OleDbDataAdapter _Adapter = new OleDbDataAdapter(_command);
                _Adapter.Fill(_DataTable);
                cn.Close();
            }
            catch (System.Exception ex)
            { MessageBox.Show("Lỗi " + ex.Message); }
            return _DataTable;
        }
        private bool CheckData(DataTable Table, string nguyenLieu)
        {
            foreach (DataRow dr in Table.Rows)
            {
                if (dr["Tenthucan"].ToString() == nguyenLieu)
                {
                    return false;
                }
            }
            return true;
        }
        private void UpdateData(DataTable Table, string Name, string Fields, string Value)
        {
            foreach (DataRow dr in Table.Rows)
            {
                if (dr["Tenthucan"].ToString() == Name)
                {
                    dr[Fields] = Value;
                }
            }
        }
    }
}
